package com.example.demo.leetcode.jianzhi;

import java.util.Arrays;

/**
 * @author xujimou
 * @version 2.0
 * @Description
 * @date 2021/5/19 16:56
 */
public class Array_二维数组中查找 {

    public boolean findNumberIn2DArray(int[][] matrix, int target) {

        if(matrix.length == 0){
            return false;
        }
        int n = matrix.length; // 高
        int m =matrix[0].length;
        int [][] temp = new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                temp[i][j]=-1;
            }
        }
        return digui(n/2,m/2,matrix,temp,target);
    }

    public boolean digui(int x,int y,int [][] matrix,int [][] temp, int target){
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        boolean flag4 = false;
        boolean flag5 = false;
        if(isAvailble(x,y,temp)){
            temp[x][y] = 1;
            if(target == matrix[x][y]){
                flag1 = true;
            }else if(target > matrix[x][y]){
                flag2 = digui(x+1,y,matrix,temp,target);
                flag3 = digui(x,y+1,matrix,temp,target);
            }else{
                flag4 = digui(x-1,y,matrix,temp,target);
                flag5 = digui(x,y-1,matrix,temp,target);
            }


        }
        return flag1 || flag2 || flag3 || flag4 || flag5;
    }

    public boolean isAvailble(int x, int y,int [][] temp){
        if(x<0 || x>=temp.length || y<0 || y>=temp[0].length || temp[x][y] !=-1){
            return false;
        }
        return true;
    }

}
